package labox.innovation.loginserver.gameserverpackets;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import labox.innovation.db.core.L2DatabaseFactory;

public class CharacterCountsRequest extends ClientBasePacket{

	/**
	 * 判断记录是否存在
	 * @param gameServerID
	 * @param account
	 * @return
	 */
	private boolean isRecordExists(int gameServerID, String account) {
		boolean result = false;
		Connection con = null;
		PreparedStatement statement = null;
		
		try {
			con = L2DatabaseFactory.getInstance().getConnection();
			statement = con.prepareStatement("SELECT * FROM charactercountsofaccount where server_id = ? and account = ? ");
			statement.setInt(1, gameServerID);
			statement.setString(2, account);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				result = true;
			rs.close();
		} catch(SQLException e) {
			e.printStackTrace();
		} finally {
			try{
				statement.close();
				con.close();
			} catch(Exception e) {
				e.printStackTrace();
			}
			
		}
		
		return result;
	}
	
	/**
	 * 更新记录
	 * @param gameServerID
	 * @param account
	 * @param count
	 * @return
	 */
	private boolean updateRecord(int gameServerID, String account, int count) {
		boolean result = false;
		Connection con = null;
		PreparedStatement statement = null;
		
		try {
			con = L2DatabaseFactory.getInstance().getConnection();
			statement = con.prepareStatement("update charactercountsofaccount set count = ? where server_id = ? and account = ? ");
			statement.setInt(1, count);
			statement.setInt(2, gameServerID);
			statement.setString(3, account);
			statement.executeUpdate();
			result = true;
		} catch(SQLException e) {
			e.printStackTrace();
		} finally {
			try{
				statement.close();
				con.close();
			} catch(Exception e) {
				e.printStackTrace();
			}
			
		}
		
		return result;
	} 
	
	/**
	 * 创建记录
	 * @param gameServerID
	 * @param account
	 * @param count
	 * @return
	 */
	private boolean insertRecord(int gameServerID, String account, int count) {
		boolean result = false;
		Connection con = null;
		PreparedStatement statement = null;
		
		try {
			con = L2DatabaseFactory.getInstance().getConnection();
			statement = con.prepareStatement("insert into charactercountsofaccount (server_id, account, count) values (?, ?, ?)");
			statement.setInt(1, gameServerID);
			statement.setString(2, account);
			statement.setInt(3, count);
			statement.executeUpdate();
			result = true;
		} catch(SQLException e) {
			e.printStackTrace();
		} finally {
			try{
				statement.close();
				con.close();
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	/**
	 * 存储数据
	 * @param gameServerID
	 * @param account
	 * @param count
	 */
	private void saveRecord(int gameServerID, String account, int count) {
		if (isRecordExists(gameServerID, account))
			updateRecord(gameServerID, account, count);
		else
			insertRecord(gameServerID, account, count);
	}
	
	public CharacterCountsRequest(byte[] decrypt) {
		super(decrypt);
		int gameServerID = readUnByte();
		String account = readString();
		int characterCounts = readUnByte();
		
		saveRecord(gameServerID, account, characterCounts);
	}
}
